package com.tianhui.consignortwofei.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.interf.IChartViewInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * @author fgs
 * @date 2021/2/23
 * @description
 **/
public class BarChartView extends LinearLayout {
    private TextView titleTextView;
    private BarChart barChart;
    private int barColor;
    private List<String> barLabelList = new ArrayList<>();

    public BarChartView(Context context) {
        this(context, null);
    }

    public BarChartView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BarChartView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.BarChartView);
        String title;

        try {
            title = typedArray.getString(R.styleable.BarChartView_BarChartViewTitle);
            barColor = typedArray.getColor(R.styleable.BarChartView_BarChartViewBarColor, getResources().getColor(R.color.colorSeven));
        } finally {
            typedArray.recycle();
        }
        View view = LayoutInflater.from(context).inflate(R.layout.layout_bar_chart_view, null);
        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(layoutParams);
        addView(view);

        titleTextView = view.findViewById(R.id.layout_bar_chart_view_titleTextView);
        barChart = view.findViewById(R.id.layout_bar_chart_view_barChartView);

        setBarChartTitle(title);

        initBarChartView();
    }

    public void setBarChartTitle(String title) {
        titleTextView.setText(title);
    }

    private void initBarChartView() {
        XAxis xAxis = barChart.getXAxis();
        //是否显示横轴值
        xAxis.setDrawLabels(true);
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawAxisLine(true);
        xAxis.setDrawGridLines(false);
        xAxis.setLabelCount(7);
        xAxis.setLabelRotationAngle(-45);
        xAxis.setValueFormatter(new XValueFormatter());

        YAxis left = barChart.getAxisLeft();
        left.setDrawLabels(true); // no axis labels
        left.setDrawAxisLine(true); // no axis line
        left.setDrawGridLines(false); // no grid lines
        barChart.getAxisRight().setEnabled(false);


        barChart.getAxisLeft().setDrawGridLines(false);
        barChart.getLegend().setEnabled(false);
        barChart.setDescription(null);
        barChart.animateY(2000);
        //允许你打开或者关闭与图表的所有触摸交互的情况
        barChart.setTouchEnabled(true);

    }

    private class XValueFormatter extends ValueFormatter {


        @Override
        public String getAxisLabel(float value, AxisBase axis) {

            int index = (int) value;
            if (index < 0 || index >= barLabelList.size()) {
                return "";
            } else {
                return barLabelList.get(index);
            }
        }
    }

    public void setBarChartData(List<IChartViewInfo> infoList) {
        if (infoList == null) return;
        barLabelList.clear();
        ArrayList<BarEntry> values = new ArrayList<>();
        int size = infoList.size();
        IChartViewInfo viewInfo;
        if (size > 0) {

            XAxis xAxis = barChart.getXAxis();
            if (size > 15) {
                xAxis.setLabelCount(15);
            } else {
                xAxis.setLabelCount(size);
            }

            for (int i = 0; i < size; i++) {
                viewInfo = infoList.get(i);
                float value = Float.valueOf(viewInfo.getValue());
                values.add(new BarEntry(i, value));
                barLabelList.add(viewInfo.getLabel());
            }

            BarDataSet set1;
            if (barChart.getData() != null &&
                    barChart.getData().getDataSetCount() > 0) {
                set1 = (BarDataSet) barChart.getData().getDataSetByIndex(0);
                set1.setValues(values);
                barChart.getData().notifyDataChanged();
                barChart.notifyDataSetChanged();
            } else {
                set1 = new BarDataSet(values, "");
                int startColor = ContextCompat.getColor(getContext(), R.color.colorBlueLight);
                int endColor = ContextCompat.getColor(getContext(), R.color.colorBlue);

                set1.setGradientColor(startColor, endColor);
                set1.setColor(barColor);
                ArrayList<IBarDataSet> dataSets = new ArrayList<>();
                dataSets.add(set1);

                BarData data = new BarData(dataSets);
                data.setValueTextSize(10f);
                //设置柱状的宽度
                data.setBarWidth(0.3f);
                barChart.setData(data);
                barChart.setFitBars(true);
            }

            set1.setValueFormatter(new ValueFormatter() {
                @Override
                public String getFormattedValue(float value) {
//                    int result = (int) value;
                    String result = value + "";
                    return result + "";
                }
            });

            barChart.invalidate();
        }

    }
}
